const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const lodash = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const DATA1 = require("../test/210220_safetyzonevalue-gaoshou.json");
const DATA2 = require("../test/210220_验证我的safetyzonevalue.json")

mongoConnected.then(async $ => {
  let yulinluodian = await mongoModels.TB21_luodian.find({
    map_name: "Savage_Main"
  })
  for (let d of yulinluodian) {
    let match_id = d.match_id;
    let player_name = d.player_name;
    let accountId = d.account_id;
    let matchThe = (await mongoModels.Match.findOne({ match_id: match_id }))
    let matchStartTime = matchThe['start_time'];
    let matchEndTime = matchThe['end_time']
    let player_counts = matchThe.players.length;
    let killed = (await mongoModels.Event.find({
      match_id: match_id,
      _T: "LogPlayerKill"
    })).sort((a, b) => a._D.valueOf() - b._D.valueOf());
    let meKilledIndex = killed.findIndex(e => e._doc.victim.accountId == accountId);
    if (meKilledIndex >= 0) {
      let killEvt = killed[meKilledIndex];
      let survivedTime = killEvt._D.valueOf() - matchStartTime.valueOf();
      d['dev'] = d['dev'] || {};
      d['dev'] = {
        ...d['dev'],
        killed: true,
        survivTime: survivedTime,
        killShunxu: meKilledIndex + 1
      }
    } else {
      d['dev'] = d['dev'] || {};
      d['dev'] = {
        ...d['dev'],
        killed: false,
        survivTime: matchEndTime.valueOf() - matchStartTime.valueOf(),
        killShunxu: player_counts
      }
    }
    await d.save();
    // debugger
  }
  return process.exit();
  debugger
  let lanse = DATA2.filter(e => e.safetyzone_value >= 20);
  for (let d of lanse) {
    let r = await mongoModels.TB21_luodian.find({
      player_name: d.player_name,
      ranking: d.ranking,
      "luodian.x": d.luodian.x
    });
    if (r.length > 1) {
      debugger
    }
    let match_id = r[0].match_id;
    let player_name = r[0].player_name;
    let accountId = r[0].account_id
    let matchThe = (await mongoModels.Match.findOne({ match_id: match_id }))
    let matchStartTime = matchThe['start_time'];
    let matchEndTime = matchThe['end_time']
    let player_counts = matchThe.players.length;
    // debugger
    let killed = (await mongoModels.Event.find({
      match_id: match_id,
      _T: "LogPlayerKill"
    })).sort((a, b) => a._D.valueOf() - b._D.valueOf());
    let meKilledIndex = killed.findIndex(e => e._doc.victim.accountId == accountId);
    if (meKilledIndex >= 0) {
      let killEvt = killed[meKilledIndex];

      let survivedTime = killEvt._D.valueOf() - matchStartTime.valueOf();
      d['dev'] = {
        killed: true,
        survivTime: survivedTime,
        killShunxu: meKilledIndex + 1
      }
      // debugger
    } else {
      if (r[0].ranking != 1) {
        debugger
      }
      d['dev'] = {
        killed: false,
        survivTime: matchEndTime.valueOf() - matchStartTime.valueOf(),
        killShunxu: player_counts
      }
    }
    // debugger
  }
  await fs.writeFile(path.join(__dirname, "../test/210221test01.json"), JSON.stringify(lanse));
  debugger
})